﻿/// <reference path="../jquery-1.4.4-vsdoc.js" />
/// <reference path="jquery.cookie.js" />
/// <reference path="jquery.mvc.js" />
/// <reference path="jquery.message.js" />

$.login = {
    event: "close keydown login",
    show: function () {
        $.mask.show();
        $.message.create({
            msg: "<div class=\"lg_load title\">加载中...</div>"
        }).css({ backgroundColor: "#fff" }).center().fadeIn(function () {
            var _this = $(this)

            $.ajax(
            {
                url: "/content/template/login.html",
                type: "GET",
                error: function () {
                    $(".lg_load").html("系统错误! 请稍候重试");
                    setTimeout(function () { $.login.clear(true) }, 5000);
                },
                success: function (result) {
                    _this.html(result);
                    _this.center();
                    _this.find("#username").focus();

                    _this.bind($.login.event, { source: _this }, $.login.process);
                }
            });
        });
    },
    process: function (event) {
        var _msg = event.data.source.find(".msg");

        switch (event.type) {
            case "keydown":
                if (event.keyCode == 27)
                    event.data.source.trigger("close");
                else if (event.keyCode == 13)
                    $(event.data.source).trigger("login");
                break;
            case "login":

                if (event.data.source.find("#username").val() == String.empty) {
                    $.message({ msg: "用户名不能为空.", to: _msg, timer: 5000 });
                    event.data.source.find("#username").focus();
                    return;
                }

                if (event.data.source.find("#password").val() == String.empty) {
                    $.message({ msg: "密码不能为空.", to: _msg, timer: 5000 });
                    event.data.source.find("#password").focus();
                    return;
                }

                var _timer = setTimeout(function () { $.message.create({ msg: "登录中...", to: _msg }); }, 500);

                $.ajax(
                {
                    url: $.mvc.action("Login", $.mvc.controllername()),
                    type: "POST",
                    data:
                    {
                        username: event.data.source.find("#username").val(),
                        password: event.data.source.find("#password").val()
                    },
                    complete: function () {
                        clearTimeout(_timer);
                    },
                    error: function () {
                        $.message.create({ msg: "系统错误! 请稍候重试.", to: _msg, timer: 10000 });
                    },
                    success: function (message) {
                        if (message && message.flag) {
                            for (var item in message.data)
                                $.cookie(item, message.data[item], { path: "/" });

                            event.data.source.trigger("close");
                        } else {
                            if (message.msg)
                                $.message.create({ msg: message.msg, to: _msg, timer: 5000 });
                            else
                                $.message.create({ msg: "系统错误! 请稍候重试", to: _msg, timer: 5000 });
                        }
                    }
                });
                break;
        }
    }
};